<!DOCTYPE html>
<html lang="zh-hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/wow/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/wow/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/wow/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/wow/images/logo.svg" color="#222">

<link rel="stylesheet" href="/wow/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"single-flower.gitee.io","root":"/wow/","images":"/wow/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"بحث...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta property="og:type" content="website">
<meta property="og:title" content="朵朵最可爱">
<meta property="og:url" content="https://single-flower.gitee.io/wow/page/2/index.html">
<meta property="og:site_name" content="朵朵最可爱">
<meta property="og:locale">
<meta property="article:author" content="X y">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://single-flower.gitee.io/wow/page/2/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-hans'
  };
</script>
<title>朵朵最可爱</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="تشغيل شريط التصفح" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/wow/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">朵朵最可爱</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/wow/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-about"><a href="/wow/about/" rel="section"><i class="fa fa-user fa-fw"></i>معلومات</a></li>
        <li class="menu-item menu-item-tags"><a href="/wow/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>الوسوم</a></li>
        <li class="menu-item menu-item-categories"><a href="/wow/categories/" rel="section"><i class="fa fa-th fa-fw"></i>التصنيفات</a></li>
        <li class="menu-item menu-item-archives"><a href="/wow/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>الأرشيفات</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          المحتويات
        </li>
        <li class="sidebar-nav-overview">
          عام
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">X y</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/wow/archives/">
        
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">المقالات</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">التصنيفات</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">الوسوم</span>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/02/18/zy/model%E5%AE%9E%E7%8E%B0%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/wow/2021/02/18/zy/model%E5%AE%9E%E7%8E%B0%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB/" class="post-title-link" itemprop="url">model实现无限极分类</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-02-18 18:32:49" itemprop="dateCreated datePublished" datetime="2021-02-18T18:32:49+08:00">2021-02-18</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:41" itemprop="dateModified" datetime="2021-04-13T15:05:41+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="什么是无限极分类"><a href="#什么是无限极分类" class="headerlink" title="什么是无限极分类"></a>什么是无限极分类</h2><p>一个类可以分成多个子类，然后一个子类又可以分另外多个子类这样无限分下去<br>就好像windows可以新建一个文件夹，然后在这个文件夹里又可以建一些文件夹，在文件夹底下还可以建一些文件夹一样</p>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><h3 id="Django端"><a href="#Django端" class="headerlink" title="Django端"></a>Django端</h3><p>创建应用APP</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">py manage.py startapp InfinitusCate</span><br></pre></td></tr></table></figure>
<p>在settings注册声明</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">INSTALLED_APPS = [</span><br><span class="line">    <span class="string">&#x27;django.contrib.admin&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;django.contrib.auth&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;django.contrib.contenttypes&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;django.contrib.sessions&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;django.contrib.messages&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;django.contrib.staticfiles&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;rest_framework&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;corsheaders&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;InfinitusCate&#x27;</span>,  <span class="comment">// 应用</span></span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<p>打开model.py 创建自关联模型类，并进行迁移</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django.db <span class="keyword">import</span> models</span><br><span class="line"></span><br><span class="line">class TeacherModel(models.Model):</span><br><span class="line">    name=models.CharField(max_length=<span class="number">20</span>,verbose_name=<span class="string">&#x27;分类名&#x27;</span>)</span><br><span class="line">    tid=models.ForeignKey(to=<span class="string">&#x27;self&#x27;</span>, on_delete=models.CASCADE, <span class="literal">null</span>=True, blank=True)</span><br><span class="line"></span><br><span class="line">    class Meta:</span><br><span class="line">        db_table=<span class="string">&#x27;teacher&#x27;</span></span><br><span class="line"></span><br><span class="line">    def __str__(self):</span><br><span class="line">        <span class="keyword">return</span> self.name</span><br></pre></td></tr></table></figure>
<p>py manage.py makemigrations<br>py manage.py migrate</p>
<h3 id="创建序列化器"><a href="#创建序列化器" class="headerlink" title="创建序列化器"></a>创建序列化器</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> rest_framework.serializers <span class="keyword">import</span> ModelSerializer</span><br><span class="line"><span class="keyword">from</span> InfinitusCate.models <span class="keyword">import</span> TeacherModel</span><br><span class="line"></span><br><span class="line">class TeacherSer(ModelSerializer):</span><br><span class="line">    class Meta:</span><br><span class="line">        model=TeacherModel</span><br><span class="line">        fields=<span class="string">&#x27;__all__&#x27;</span></span><br></pre></td></tr></table></figure>
<h3 id="在视图函数views-py里面写"><a href="#在视图函数views-py里面写" class="headerlink" title="在视图函数views.py里面写"></a>在视图函数views.py里面写</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">def Teach(id):</span><br><span class="line">    # 接收id 查询集</span><br><span class="line">    queryset = User.objects.filter(uid=id)</span><br><span class="line">    # 进行序列化</span><br><span class="line">    data = UserSer(queryset, many=True).data</span><br><span class="line">    # 在序列化结果中添加下一级空列表</span><br><span class="line">    data[<span class="string">&#x27;list&#x27;</span>] = []</span><br><span class="line">    # 遍历，递归调用并将调用结果存入上级列表</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> data:</span><br><span class="line">        data[<span class="string">&#x27;list&#x27;</span>].append(Teach(i.uid))</span><br><span class="line">    # 返回</span><br><span class="line">    <span class="keyword">return</span> data</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class Teacher(APIView):</span><br><span class="line">    def get(self, request):</span><br><span class="line">        # 定义列表</span><br><span class="line">        Teacher_list = []</span><br><span class="line">        # 查取所有信息</span><br><span class="line">        queryset = TeacherModel.objects.all()</span><br><span class="line">        # 遍历开始递归查询</span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> data:</span><br><span class="line">            Teacher_list.append(Teach(i.id))</span><br><span class="line">        Response(Teacher_list)</span><br></pre></td></tr></table></figure>
<h2 id="最后记得配置路由哦"><a href="#最后记得配置路由哦" class="headerlink" title="最后记得配置路由哦"></a>最后记得配置路由哦</h2>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/02/18/zy/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/wow/2021/02/18/zy/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/" class="post-title-link" itemprop="url">redis键空间事件</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-02-18 18:23:29" itemprop="dateCreated datePublished" datetime="2021-02-18T18:23:29+08:00">2021-02-18</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:44" itemprop="dateModified" datetime="2021-04-13T15:05:44+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="redis键空间通知"><a href="#redis键空间通知" class="headerlink" title="redis键空间通知"></a>redis键空间通知</h1><h3 id="接收键空间通知情况一："><a href="#接收键空间通知情况一：" class="headerlink" title="接收键空间通知情况一："></a>接收键空间通知情况一：</h3><p>接收指定键值所执行的命令，在配置文件中体现是：notify-keyspace-events K[命令集]；K代表是监听键值，监听其中命令集包括了以下几项：</p>
<p>g ： 常规命令 del expire rename等等</p>
<p>$： 字符串命令</p>
<p>l ： 列表命令</p>
<p>s：集合命令</p>
<p>h：hash命令</p>
<p>z：有序集合命令</p>
<p>x ：超时命令</p>
<h3 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h3><p>在配置文件（redis.conf）中设置：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">#notify-keyspace-events <span class="string">&quot;&quot;</span></span><br><span class="line">notify-keyspace-events K$</span><br><span class="line">（表明我们需要监听字符串所执行的命令）</span><br></pre></td></tr></table></figure>
<p>客户端一监听</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; SUBSCRIBE __keyspace@0__:message</span><br><span class="line">Reading messages... (press Ctrl-C to quit)</span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;subscribe&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__keyspace@0__:message&quot;</span></span><br><span class="line"><span class="number">3</span>) (integer) <span class="number">1</span></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;message&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__keyspace@0__:message&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;set&quot;</span></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;message&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__keyspace@0__:message&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;append&quot;</span></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;message&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__keyspace@0__:message&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;append&quot;</span>          </span><br></pre></td></tr></table></figure>
<p>客户端2执行</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; set message <span class="number">123</span></span><br><span class="line">OK</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; get message</span><br><span class="line"><span class="string">&quot;123&quot;</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; append message <span class="string">&quot;-3444555&quot;</span></span><br><span class="line">(integer) <span class="number">11</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; append message <span class="string">&quot;-3444555&quot;</span></span><br><span class="line">(integer) <span class="number">19</span></span><br></pre></td></tr></table></figure>
<p>可以看出例子1:我们执行 修改的命令有：set append 都在客户端1进行返回。</p>
<h3 id="接收键空间通知情况二："><a href="#接收键空间通知情况二：" class="headerlink" title="接收键空间通知情况二："></a>接收键空间通知情况二：</h3><p>监控指定的命令，配置文件中体现：notify-keyspace-events E[命令集] ;E代表监听命令，后面的命令集合上面的一样</p>
<h3 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h3><p>在配置文件（redis.conf）中设置：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">#notify-keyspace-events <span class="string">&quot;&quot;</span></span><br><span class="line">notify-keyspace-events Elg</span><br><span class="line">（表明我们监控list列表基础命令）</span><br></pre></td></tr></table></figure>
<p>客户端1监听：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; SUBSCRIBE __keyevent@0__:del</span><br><span class="line">Reading messages... (press Ctrl-C to quit)</span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;subscribe&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__keyevent@0__:del&quot;</span></span><br><span class="line"><span class="number">3</span>) (integer) <span class="number">1</span></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;message&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__keyevent@0__:del&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;list2&quot;</span></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;message&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__keyevent@0__:del&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;mylist1&quot;</span></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;message&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__keyevent@0__:del&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;list1&quot;</span></span><br></pre></td></tr></table></figure>
<p>客户端2执行：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; keys *</span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;list2&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;mylist1&quot;</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; del list2 mylist1</span><br><span class="line">(integer) <span class="number">2</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; keys *</span><br><span class="line">(empty list or set)</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; LPUSH list1 abc</span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; LPUSH list1 abc def</span><br><span class="line">(integer) <span class="number">3</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; LRANGE list1 <span class="number">0</span> -<span class="number">1</span></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;def&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;abc&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;abc&quot;</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; del list1</span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; </span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/02/11/zy/webshell/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/wow/2021/02/11/zy/webshell/" class="post-title-link" itemprop="url">webshell</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-02-11 14:22:11" itemprop="dateCreated datePublished" datetime="2021-02-11T14:22:11+08:00">2021-02-11</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:50" itemprop="dateModified" datetime="2021-04-13T15:05:50+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="什么是webshell"><a href="#什么是webshell" class="headerlink" title="什么是webshell"></a>什么是webshell</h2><p>顾名思义，”web” - 显然需要服务器开放web服务,”shell” - 取得对服务器某种程度上操作权限<br>webshell常常被称为匿名用户(入侵者)通过web服务端口对web服务器有某种程度上操作的权限,由于其大多是以网页脚本的形式出现，也有人称之为网站后门工具</p>
<h3 id="浏览器实现xshell的功能，实现黑窗口"><a href="#浏览器实现xshell的功能，实现黑窗口" class="headerlink" title="浏览器实现xshell的功能，实现黑窗口"></a>浏览器实现xshell的功能，实现黑窗口</h3><h3 id="什么是shell？"><a href="#什么是shell？" class="headerlink" title="什么是shell？"></a>什么是shell？</h3><p>shell是一个集成了一堆linux命令的控制台界面</p>
<h3 id="shell只有这一种吗？"><a href="#shell只有这一种吗？" class="headerlink" title="shell只有这一种吗？"></a>shell只有这一种吗？</h3><p>SH：编程方面相当优秀，输入命令优秀<br>BASH：Linux系统中一个默认的Shell，命令特别多</p>
<h2 id="webshell的优点"><a href="#webshell的优点" class="headerlink" title="webshell的优点"></a>webshell的优点</h2><p>webshell 最大的优点就是可以穿越防火墙，由于与被控制的服务器或远程主机交换的数据都是通过80端口传递的，因此不会被防火墙拦截。并且使用webshell一般不会在系统日志中留下记录，只会在网站的web日志中留下一些数据提交记录，没有经验的管理员是很难看出入侵痕迹的</p>
<h2 id="webshell的分类"><a href="#webshell的分类" class="headerlink" title="webshell的分类"></a>webshell的分类</h2><h3 id="根据文件大小分类"><a href="#根据文件大小分类" class="headerlink" title="根据文件大小分类"></a>根据文件大小分类</h3><p>大马<br>体积大，包含很多功能<br>通常会调用系统的关键函数<br>通常以代码加密隐藏<br>小马<br>一句话木马也称为小马，即整个shell代码量只有一行，一般时系统执行函数</p>
<h3 id="根据脚本名称分类"><a href="#根据脚本名称分类" class="headerlink" title="根据脚本名称分类"></a>根据脚本名称分类</h3><p>jsp、asp、aspx、php</p>
<h2 id="webshell的功能"><a href="#webshell的功能" class="headerlink" title="webshell的功能"></a>webshell的功能</h2><h3 id="站长工具"><a href="#站长工具" class="headerlink" title="站长工具"></a>站长工具</h3><p>Webshell被站长常常用于网站管理、服务器管理等，根据FSO权限的不同，作用有在线编辑网页脚本、上传下载文件、查看数据库、执行任意程序命令等</p>
<h3 id="持续远程访问"><a href="#持续远程访问" class="headerlink" title="持续远程访问"></a>持续远程访问</h3><p>入侵者可以利用从而达到长期控制网站服务器的目的，若攻击者自行修复了漏洞，以确保没有其他人会利用该漏洞，攻击者可以低调的随时控制服务器。一些流行的webshell使用密码验证和其他技术来确保只有上传webshell的攻击者才能访问它</p>
<h3 id="权限提升"><a href="#权限提升" class="headerlink" title="权限提升"></a>权限提升</h3><p>在服务器没有配置错误的情况下，webshell将在web服务器的用户权限下运行，该用户权限是有限的。通过使用webshell，攻击者可以尝试通过利用系统上的本地漏洞来执行权限提升</p>
<h3 id="极强的隐蔽性"><a href="#极强的隐蔽性" class="headerlink" title="极强的隐蔽性"></a>极强的隐蔽性</h3><p>有些恶意网页脚本可以嵌套在正常网页中运行，且不容易被查杀。webshell还可以穿越服务器防火墙，由于与被控制的服务器或远程主机交互的数据都是通过80端口传递，因此不会被防火墙拦截，在没有记录流量的情况下，webshell使用post包发送，也不会被记录在系统日志中，只会在web日志中记录一些数据提交的记录</p>
<h2 id="Vue端"><a href="#Vue端" class="headerlink" title="Vue端"></a>Vue端</h2><h3 id="安装xterm"><a href="#安装xterm" class="headerlink" title="安装xterm"></a>安装xterm</h3><p>cnpm install <a href="mailto:&#x78;&#x74;&#101;&#x72;&#x6d;&#64;&#51;&#x2e;&#49;&#x2e;&#48;">&#x78;&#x74;&#101;&#x72;&#x6d;&#64;&#51;&#x2e;&#49;&#x2e;&#48;</a> –save</p>
<h3 id="导入css样式，main-js"><a href="#导入css样式，main-js" class="headerlink" title="导入css样式，main.js"></a>导入css样式，main.js</h3><p>import ‘xterm/dist/xterm.css’ // 看这里，添加xterm css文件样式</p>
<h3 id="在页面里放入一个id对的上的div"><a href="#在页面里放入一个id对的上的div" class="headerlink" title="在页面里放入一个id对的上的div"></a>在页面里放入一个id对的上的div</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=<span class="string">&quot;terminal&quot;</span>&gt;</span><br><span class="line">&lt;!--            黑窗口--&gt;</span><br><span class="line">&lt;/div&gt;</span><br></pre></td></tr></table></figure>
<h3 id="添加插件"><a href="#添加插件" class="headerlink" title="添加插件"></a>添加插件</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; Terminal &#125; <span class="keyword">from</span> <span class="string">&#x27;xterm&#x27;</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> attach <span class="keyword">from</span> <span class="string">&#x27;xterm/lib/addons/attach/attach&#x27;</span> <span class="comment">// 安装插件适，可以使用attach去添加</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> fit <span class="keyword">from</span> <span class="string">&#x27;xterm/lib/addons/fit/fit&#x27;</span> <span class="comment">// fit进行自适应大小的</span></span><br><span class="line"></span><br><span class="line">Terminal.applyAddon(attach) <span class="comment">// 添加插件</span></span><br><span class="line">Terminal.applyAddon(fit) <span class="comment">// 添加插件</span></span><br></pre></td></tr></table></figure>
<h3 id="mounted初始化div标签id为terminal对象，成为一个真正的黑窗口"><a href="#mounted初始化div标签id为terminal对象，成为一个真正的黑窗口" class="headerlink" title="mounted初始化div标签id为terminal对象，成为一个真正的黑窗口"></a>mounted初始化div标签id为terminal对象，成为一个真正的黑窗口</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">mounted</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="comment">// 获取到了div标签</span></span><br><span class="line">        <span class="keyword">let</span> terminalContainer = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;terminal&#x27;</span>)</span><br><span class="line">        <span class="comment">// 初始化黑窗口对象</span></span><br><span class="line">        <span class="built_in">this</span>.term = <span class="keyword">new</span> Terminal(<span class="built_in">this</span>.terminal)</span><br><span class="line">        <span class="comment">// 打开这个对象</span></span><br><span class="line">        <span class="built_in">this</span>.term.open(terminalContainer)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">new</span> WebSocket(<span class="string">&#x27;ws://127.0.0.1:8000/webssh/&#x27;</span>)</span><br><span class="line">        <span class="built_in">this</span>.terminalSocket = <span class="keyword">new</span> WebSocket(<span class="string">&#x27;ws://127.0.0.1:8000/webssh/&#x27;</span>)</span><br><span class="line">        <span class="built_in">this</span>.terminalSocket.onopen = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; <span class="comment">// 连接成功触发该方法</span></span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;websocket is Connected...&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.terminalSocket.onclose = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; <span class="comment">// 连接关闭适触发的方法</span></span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;websocket is Closed...&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.terminalSocket.onerror = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; <span class="comment">// 连接出错触发的方法</span></span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;damn Websocket is broken!&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.term.attach(<span class="built_in">this</span>.terminalSocket)</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="Django端"><a href="#Django端" class="headerlink" title="Django端"></a>Django端</h2><h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><p>pip install dwebsocket 让django支持ws连接<br>pip install paramiko 连接远程服务器</p>
<h3 id="paramiko连接linux"><a href="#paramiko连接linux" class="headerlink" title="paramiko连接linux"></a>paramiko连接linux</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> dwebsocket <span class="keyword">import</span> accept_websocket</span><br><span class="line"><span class="keyword">import</span> paramiko</span><br><span class="line">def make_ssh(host, username, password, port=<span class="number">22</span>):</span><br><span class="line">    <span class="string">&quot;&quot;</span><span class="string">&quot;</span></span><br><span class="line"><span class="string">    :host 主机地址</span></span><br><span class="line"><span class="string">    :username 用户名，一般是root</span></span><br><span class="line"><span class="string">    :password 密码</span></span><br><span class="line"><span class="string">    :port ssh协议的端口,22</span></span><br><span class="line"><span class="string">    &quot;</span><span class="string">&quot;&quot;</span></span><br><span class="line">    # 初始化一个ssh对象</span><br><span class="line">    sh = paramiko.SSHClient()</span><br><span class="line">    # 设置对象连接密钥规则</span><br><span class="line">    sh.set_missing_host_key_policy(paramiko.AutoAddPolicy())</span><br><span class="line">    # 连接</span><br><span class="line">    sh.connect(host, username=username, password=password)</span><br><span class="line">    # 生成shell对象，把命令执行结果，模拟成一个终端的返回，带上花里胡哨的颜色</span><br><span class="line">    channle = sh.invoke_shell(term=<span class="string">&#x27;xterm&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> channle</span><br></pre></td></tr></table></figure>
<h3 id="创建子线程-接收远程服务器"><a href="#创建子线程-接收远程服务器" class="headerlink" title="创建子线程 接收远程服务器"></a>创建子线程 接收远程服务器</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">def recv_ssh_msg(channle, ws):</span><br><span class="line">    <span class="string">&#x27;&#x27;</span><span class="string">&#x27;</span></span><br><span class="line"><span class="string">        channle: 建立好的SSH连接通道</span></span><br><span class="line"><span class="string">        这个函数会不停的接收ssh通道返回的命令</span></span><br><span class="line"><span class="string">        返回到前端的ws套接字里</span></span><br><span class="line"><span class="string">    &#x27;</span><span class="string">&#x27;&#x27;</span></span><br><span class="line">    # 判断shell连接对象是否没有退出</span><br><span class="line">    <span class="keyword">while</span> not channle.exit_status_ready():</span><br><span class="line">      	# 接收过程可能会因为没有任何返回而报错</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            buf = channle.recv(1024) # 接收命令的执行结果</span><br><span class="line">            ws.send(buf) # 向Websocket通道返回</span><br><span class="line">        # 接收不到会报错，但是报错没关系，继续重新尝试接受</span><br><span class="line">        except:</span><br><span class="line">            <span class="keyword">break</span></span><br></pre></td></tr></table></figure>
<h3 id="创建视图函数进行连接"><a href="#创建视图函数进行连接" class="headerlink" title="创建视图函数进行连接"></a>创建视图函数进行连接</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line">@accept_websocket</span><br><span class="line">def webssh(request):</span><br><span class="line">    <span class="keyword">if</span> request.is_websocket:</span><br><span class="line">        # 1. 获取到连接对象</span><br><span class="line">        ws = request.websocket</span><br><span class="line">        # 2. 初始化linux连接</span><br><span class="line">        channel = make_ssh()</span><br><span class="line">        # 3. 初始化linux数据接收线程，并开启</span><br><span class="line">        recv_thread = Thread(target=recv_ssh_msg, args=(channel, ws))</span><br><span class="line">        recv_thread.start()</span><br><span class="line">        while 1:  # 主线程: 只管发送</span><br><span class="line">            # 2. 阻塞ws接收发来的数据</span><br><span class="line">            cmd = ws.wait()</span><br><span class="line">            # cmd = ws.recv()</span><br><span class="line">            <span class="keyword">if</span> cmd:</span><br><span class="line">                channel.send(cmd)  # 发送到linux 去执行</span><br><span class="line">            else:  # 如果连接断开，那么cmd将会发一个空包</span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line">        recv_thread.join()  # 回收子线程</span><br><span class="line">        ws.close()  # 关闭ws连接</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/02/10/zy/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/wow/2021/02/10/zy/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/" class="post-title-link" itemprop="url">redis发布者订阅者</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-02-10 16:49:50" itemprop="dateCreated datePublished" datetime="2021-02-10T16:49:50+08:00">2021-02-10</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:42" itemprop="dateModified" datetime="2021-04-13T15:05:42+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="什么是Redis"><a href="#什么是Redis" class="headerlink" title="什么是Redis"></a>什么是Redis</h2><p>Redis是一个开源的内存数据库，它以键值对的形式存储数据。由于数据存储在内存中，因此Redis的速度很快，但是每次重启Redis服务时，其中的数据也会丢失，因此，Redis也提供了持久化存储机制(AOF,RDB)，将数据以某种形式保存在文件中，每次重启时，可以自动从文件加载数据到内存当中</p>
<h2 id="什么是发布订阅"><a href="#什么是发布订阅" class="headerlink" title="什么是发布订阅"></a>什么是发布订阅</h2><p>Redis发布订阅（pub/sub）是一种消息通信模式，发布者（publish）发送消息，订阅者（subscribe）接收消息<br>订阅者（如客户端）以事件订阅的方式表达出它有兴趣接收的一个事件或一类事件<br>发布者（如服务器）可将订阅者感兴趣的事件随时通知相关订阅者</p>
<h3 id="通俗说的话，就比如我sub端（订阅者）一直监听着，一旦pub端（发布者）发布了消息，那么我就接收过来"><a href="#通俗说的话，就比如我sub端（订阅者）一直监听着，一旦pub端（发布者）发布了消息，那么我就接收过来" class="headerlink" title="通俗说的话，就比如我sub端（订阅者）一直监听着，一旦pub端（发布者）发布了消息，那么我就接收过来"></a>通俗说的话，就比如我sub端（订阅者）一直监听着，一旦pub端（发布者）发布了消息，那么我就接收过来</h3><h2 id="Redis发布订阅架构"><a href="#Redis发布订阅架构" class="headerlink" title="Redis发布订阅架构"></a>Redis发布订阅架构</h2><p>Redis提供了发布订阅功能，可以用于消息的传输，Redis的发布订阅机制包括三个部分：发布者，订阅者，Channel</p>
<p>发布者和订阅者都是Redis客户端，Channel则为Redis服务器端，发布者将消息发送到某个的频道，订阅了这个频道的订阅者就能接收到这条消息。<br>Redis的这种发布订阅机制与基于主题的发布订阅类似，Channel相当于主题</p>
<h2 id="Redis发布订阅模式优点"><a href="#Redis发布订阅模式优点" class="headerlink" title="Redis发布订阅模式优点"></a>Redis发布订阅模式优点</h2><p>发送者（发布者）不是计划发送消息给特定的接收者（订阅者）。<br>而是发布的消息分到不同的频道，不需要知道什么样的订阅者订阅。<br>订阅者对一个或多个频道感兴趣，只需接收感兴趣的消息，不需要知道什么样的发布者发布的。<br>这种发布者和订阅者的解耦合可以带来更大的扩展性和更加动态的网络拓扑</p>
<h2 id="常见命令"><a href="#常见命令" class="headerlink" title="常见命令"></a>常见命令</h2><p>Psubscribe</p>
<p>Redis Psubscribe 命令订阅一个或多个符合给定模式的频道<br>Pubsub</p>
<p>Redis Pubsub 命令用于查看订阅与发布系统状态，它由数个不同格式的子命令组成<br>Publish</p>
<p>Redis Publish 命令用于将信息发送到指定的频道<br>Punsubscribe</p>
<p>Redis Punsubscribe 命令用于退订所有给定模式的频道<br>Subscribe</p>
<p>Redis Subscribe 命令用于订阅给定的一个或多个频道的信息<br>Unsubscribe</p>
<p>Redis Unsubscribe 命令用于退订给定的一个或多个频道的信息</p>
<h2 id="总结："><a href="#总结：" class="headerlink" title="总结："></a>总结：</h2><p>发布订阅是消息队列的一种方式，基于消息队列的方式，可以实现系统解耦、削峰填谷，顶住流量洪峰<br>redis的主业目前是基于键值对的数据存储、缓存等，消息队列可能是redis的一种尝试，但对redis消息队列以后的发展我们可以保持持续关注</p>
<h3 id="今天分享的博客就到这里咯-欢迎大家前来留言观看哦"><a href="#今天分享的博客就到这里咯-欢迎大家前来留言观看哦" class="headerlink" title="今天分享的博客就到这里咯 欢迎大家前来留言观看哦"></a>今天分享的博客就到这里咯 欢迎大家前来留言观看哦</h3>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/02/10/zy/axios%E5%B0%81%E8%A3%85/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/wow/2021/02/10/zy/axios%E5%B0%81%E8%A3%85/" class="post-title-link" itemprop="url">axios封装</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-02-10 15:46:04" itemprop="dateCreated datePublished" datetime="2021-02-10T15:46:04+08:00">2021-02-10</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:26" itemprop="dateModified" datetime="2021-04-13T15:05:26+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="Axios是什么"><a href="#Axios是什么" class="headerlink" title="Axios是什么"></a>Axios是什么</h2><p>Axios是一个基于promise的HTTP库，简单的讲就是可以发送get、post请求</p>
<h2 id="Axios特性"><a href="#Axios特性" class="headerlink" title="Axios特性"></a>Axios特性</h2><p>可以在浏览器中发送XMLHttpRequests<br>可以在node.js发送http请求<br>支持Promise API<br>拦截请求和响应<br>转换请求数据和响应数据<br>能够取消请求<br>自动转换JSON数据<br>客户端支持保护安全免受XSRF攻击</p>
<h2 id="Axios适用于什么场景"><a href="#Axios适用于什么场景" class="headerlink" title="Axios适用于什么场景"></a>Axios适用于什么场景</h2><p>浏览器发送请求，或者Node.js发送请求都可以用到Axios<br>像Vue、React、Node等项目就可以使用Axios<br>如果项目中用到了Jquery，那么就不需要用axios，因为Jquery本身就可以发送请求</p>
<h1 id="使用Axios"><a href="#使用Axios" class="headerlink" title="使用Axios"></a>使用Axios</h1><h2 id="安装Axios"><a href="#安装Axios" class="headerlink" title="安装Axios"></a>安装Axios</h2><p>npm install axios –save</p>
<h2 id="配置axios基本属性"><a href="#配置axios基本属性" class="headerlink" title="配置axios基本属性"></a>配置axios基本属性</h2><p>在src路径下新建一个文件夹，在该文件夹下的package.js中写入（是用来封装axios的，js文件名可以随便起）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> axios = <span class="built_in">require</span>(<span class="string">&#x27;axios&#x27;</span>);</span><br><span class="line">axios.defaults.baseURL = <span class="string">&#x27;http://127.0.0.1:8000&#x27;</span>;  <span class="comment">// 请求的接口主域名</span></span><br><span class="line">axios.defaults.timeout = <span class="number">10000</span>;  <span class="comment">// 请求超时设置10s</span></span><br></pre></td></tr></table></figure>
<h2 id="Axios拦截器"><a href="#Axios拦截器" class="headerlink" title="Axios拦截器"></a>Axios拦截器</h2><h3 id="在axios基本属性下面写"><a href="#在axios基本属性下面写" class="headerlink" title="在axios基本属性下面写"></a>在axios基本属性下面写</h3><p>请求拦截器</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">axios.interceptors.request.use(<span class="function"><span class="keyword">function</span> (<span class="params">config</span>) </span>&#123;</span><br><span class="line">　　<span class="comment">// 在发送请求之前做些什么</span></span><br><span class="line">　　<span class="keyword">return</span> config</span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span> (<span class="params">error</span>) </span>&#123;</span><br><span class="line">　　<span class="comment">// 对请求错误做些什么</span></span><br><span class="line"><span class="keyword">return</span> <span class="built_in">Promise</span>.reject(error)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="响应拦截器"><a href="#响应拦截器" class="headerlink" title="响应拦截器"></a>响应拦截器</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">axios.interceptors.response.use(<span class="function"><span class="keyword">function</span> (<span class="params">response</span>) </span>&#123;</span><br><span class="line">　　<span class="comment">// 对响应数据做点什么</span></span><br><span class="line">　　<span class="keyword">return</span> response</span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span> (<span class="params">error</span>) </span>&#123;</span><br><span class="line">　　<span class="comment">// 对响应错误做点什么</span></span><br><span class="line">　　<span class="keyword">return</span> <span class="built_in">Promise</span>.reject(error)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h2 id="Axios封装"><a href="#Axios封装" class="headerlink" title="Axios封装"></a>Axios封装</h2><p>axios封装常用的四种方法：get、post、put、delete<br>axios封装的好处：为了更好地维护，简化代码，易复用</p>
<h3 id="get方法"><a href="#get方法" class="headerlink" title="get方法"></a>get方法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * get方法，对应get请求</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;String&#125;</span> </span>url [请求的url地址]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>params [请求时携带的参数]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">get</span>(<span class="params">url, params</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span>&#123;</span><br><span class="line">        axios.get(url, &#123;</span><br><span class="line">            params: params</span><br><span class="line">        &#125;).then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">            resolve(res.data);</span><br><span class="line">        &#125;).catch(<span class="function"><span class="params">err</span> =&gt;</span>&#123;</span><br><span class="line">            reject(err.data)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;);&#125;</span><br></pre></td></tr></table></figure>
<p>我们通过定义一个get函数，get函数有两个参数，第一个参数表示我们要请求的url地址，第二个参数是我们要携带的请求参数。</p>
<h3 id="post方法"><a href="#post方法" class="headerlink" title="post方法"></a>post方法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * post方法，对应post请求</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;String&#125;</span> </span>url [请求的url地址]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>params [请求时携带的参数]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">post</span>(<span class="params">url, params</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">         axios.post(url, QS.stringify(params))</span><br><span class="line">        .then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">            resolve(res.data);</span><br><span class="line">        &#125;)</span><br><span class="line">        .catch(<span class="function"><span class="params">err</span> =&gt;</span>&#123;</span><br><span class="line">            reject(err.data)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>原理同get基本一样，但是要注意的是，post方法必须要使用对提交从参数对象进行序列化的操作</p>
<h3 id="put方法"><a href="#put方法" class="headerlink" title="put方法"></a>put方法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * put方法，对应put请求</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;String&#125;</span> </span>url [请求的url地址]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>params [请求时携带的参数]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>headers [请求时的头部]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">put</span>(<span class="params">url, params, headers</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    axios.put(url, params, headers).then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">      resolve(res.data)</span><br><span class="line">    &#125;).catch(<span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">// debugger</span></span><br><span class="line">      reject(err.data)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>更新数据</p>
<h3 id="delete方法"><a href="#delete方法" class="headerlink" title="delete方法"></a>delete方法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * delete方法，对应delete请求</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;String&#125;</span> </span>url [请求的url地址]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>params [请求时携带的参数]</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>headers [请求时的头部]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">del</span>(<span class="params">url, params, headers</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    axios.delete(url, &#123;<span class="attr">data</span>: params, headers&#125;).then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">      resolve(res.data)</span><br><span class="line">    &#125;).catch(<span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">// debugger</span></span><br><span class="line">      reject(err.data)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>删除数据</p>
<h3 id="使用axios方法"><a href="#使用axios方法" class="headerlink" title="使用axios方法:"></a>使用axios方法:</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> axios;</span><br></pre></td></tr></table></figure>
<p>package.js里面的就写好啦，下面就要在api.js里面获取api接口了</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/02/03/zy/celery%E5%BC%82%E6%AD%A5%E6%A1%86%E6%9E%B6(5.0)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/wow/2021/02/03/zy/celery%E5%BC%82%E6%AD%A5%E6%A1%86%E6%9E%B6(5.0)/" class="post-title-link" itemprop="url">celery异步框架</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-02-03 10:26:02" itemprop="dateCreated datePublished" datetime="2021-02-03T10:26:02+08:00">2021-02-03</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:36" itemprop="dateModified" datetime="2021-04-13T15:05:36+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="安装包"><a href="#安装包" class="headerlink" title="安装包"></a>安装包</h2><p>pip3 install Django==2.2</p>
<p>pip3 install celery==4.4.7</p>
<p>pip install redis=3.5.3</p>
<h2 id="今天我们以手机号发送短信为例子-具体代码如下"><a href="#今天我们以手机号发送短信为例子-具体代码如下" class="headerlink" title="今天我们以手机号发送短信为例子 具体代码如下"></a>今天我们以手机号发送短信为例子 具体代码如下</h2><h3 id="在settings中的INSTALLED-APPS里配置djcelery以及消息列队的地址"><a href="#在settings中的INSTALLED-APPS里配置djcelery以及消息列队的地址" class="headerlink" title="在settings中的INSTALLED_APPS里配置djcelery以及消息列队的地址"></a>在settings中的INSTALLED_APPS里配置djcelery以及消息列队的地址</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># settings.py</span><br><span class="line"><span class="keyword">import</span> djcelery</span><br><span class="line"></span><br><span class="line">djcelery.setup_loader()</span><br><span class="line">BROKER_URL = &#x27;redis://123.57.61.168:6379&#x27;  # 这就是broker消息队列的地址</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="创建tasks"><a href="#创建tasks" class="headerlink" title="创建tasks"></a>创建tasks</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"># tasks.py</span><br><span class="line"><span class="keyword">from</span> celery <span class="keyword">import</span> task</span><br><span class="line"><span class="keyword">from</span> ronglian_sms_sdk <span class="keyword">import</span> SmsSDK</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def SMSinit(ACCOUNT_SID, AUTH_TOKEN, APP_ID):</span><br><span class="line">    <span class="keyword">return</span> SmsSDK(ACCOUNT_SID, AUTH_TOKEN, APP_ID)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">@task</span><br><span class="line">def SMSsend(sms, tid=1, mobile=[], datas=[]):  # .delay</span><br><span class="line">    # 升级成为异步方法</span><br><span class="line">    tid = tid  # 登录验证模版 默认ID为1</span><br><span class="line">    mobile = &#x27;,&#x27;.join(mobile) if len(mobile) &gt; 1 else mobile[0]  # 接收的手机号列表</span><br><span class="line">    datas = datas</span><br><span class="line">    resp = sms.sendMessage(tid, mobile, datas)</span><br></pre></td></tr></table></figure>
<h3 id="创建utils"><a href="#创建utils" class="headerlink" title="创建utils"></a>创建utils</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># utils.py</span><br><span class="line">rom ronglian_sms_sdk <span class="keyword">import</span> SmsSDK</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class SMS:</span><br><span class="line">    def __init__(self, ACCOUNT_SID, AUTH_TOKEN, APP_ID):</span><br><span class="line">        self.sdk = SmsSDK(ACCOUNT_SID, AUTH_TOKEN, APP_ID)</span><br><span class="line"></span><br><span class="line">    def send(self, tid=<span class="number">1</span>, mobile=[], datas=[]):</span><br><span class="line">        time.sleep(<span class="number">5</span>)</span><br><span class="line">        tid = tid  # 登录验证模版 默认ID为1</span><br><span class="line">        mobile = &#x27;,&#x27;.join(mobile) if len(mobile) &gt; 1 else mobile[0]  # 接收的手机号列表</span><br><span class="line">        datas = datas</span><br><span class="line">        resp = self.sdk.sendMessage(tid, mobile, datas)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="手机号发送验证码"><a href="#手机号发送验证码" class="headerlink" title="手机号发送验证码"></a>手机号发送验证码</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"># view.py</span><br><span class="line">class PhoneSMS(APIView):</span><br><span class="line">    def post(self, request):</span><br><span class="line">        phone = request.data.get(<span class="string">&#x27;phone&#x27;</span>)</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            User.objects.get(phone=phone)</span><br><span class="line">        except:</span><br><span class="line">            <span class="keyword">return</span> Response(&#123;</span><br><span class="line">                <span class="string">&#x27;code&#x27;</span>: <span class="number">500</span>,</span><br><span class="line">                <span class="string">&#x27;message&#x27;</span>: <span class="string">&#x27;用户不存在，无法发送&#x27;</span></span><br><span class="line">            &#125;)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            # sms = SMS(ACCOUNT_SID, AUTH_TOKEN, APP_ID)</span><br><span class="line">            sms = SMSinit(ACCOUNT_SID, AUTH_TOKEN, APP_ID)</span><br><span class="line">            code = random_code(<span class="number">6</span>)</span><br><span class="line">            r.set(phone, code, <span class="number">60</span>)</span><br><span class="line">            datas = [code, <span class="string">&#x27;60s&#x27;</span>]</span><br><span class="line">            # sms.send(mobile=[phone], datas=datas)</span><br><span class="line">            SMSsend.delay(sms, mobile=[phone], datas=datas)</span><br><span class="line">            # 直接加入消息队列，由celery去管理</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> Response(&#123;</span><br><span class="line">                <span class="string">&#x27;code&#x27;</span>: <span class="number">200</span>,</span><br><span class="line">                <span class="string">&#x27;message&#x27;</span>: <span class="string">&#x27;验证码已发送&#x27;</span></span><br><span class="line">            &#125;)</span><br></pre></td></tr></table></figure>
<h3 id="最后启动（注意要保证redis为开启状态）"><a href="#最后启动（注意要保证redis为开启状态）" class="headerlink" title="最后启动（注意要保证redis为开启状态）"></a>最后启动（注意要保证redis为开启状态）</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python manage.py celery worker</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/02/01/zy/%E4%B8%89%E6%96%B9%E6%94%AF%E4%BB%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/wow/2021/02/01/zy/%E4%B8%89%E6%96%B9%E6%94%AF%E4%BB%98/" class="post-title-link" itemprop="url">三方支付</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-02-01 12:04:56" itemprop="dateCreated datePublished" datetime="2021-02-01T12:04:56+08:00">2021-02-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:19" itemprop="dateModified" datetime="2021-04-13T15:05:19+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p>1、注册一个沙箱号：<a target="_blank" rel="noopener" href="https://open.alipay.com/platform/home.htm">https://open.alipay.com/platform/home.htm</a></p>
<h2 id="一、安装插件"><a href="#一、安装插件" class="headerlink" title="一、安装插件"></a>一、安装插件</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install python-alipay-sdk --upgrade</span><br></pre></td></tr></table></figure>
<h2 id="二、在settings中配置"><a href="#二、在settings中配置" class="headerlink" title="二、在settings中配置"></a>二、在settings中配置</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">ALIPAY_APPID = <span class="string">&#x27;&#x27;</span></span><br><span class="line">ALIPAY_URL = <span class="string">&quot;&quot;</span></span><br><span class="line">ALIPAY_DEBUG = True</span><br><span class="line">APP_PRIVATE_KEY_PATH = <span class="string">&#x27;&#x27;</span></span><br><span class="line">ALIPAY_PUBLIC_KEY_PATH = <span class="string">&#x27;&#x27;</span></span><br><span class="line">ALIPAY_GATE = <span class="string">&#x27;https://openapi.alipaydev.com/gateway.do?&#x27;</span></span><br></pre></td></tr></table></figure>
<h2 id="三、发起支付"><a href="#三、发起支付" class="headerlink" title="三、发起支付"></a>三、发起支付</h2><p>请求方式：Get</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> rest_framework.views <span class="keyword">import</span> APIView</span><br><span class="line"><span class="keyword">from</span> rest_framework.response <span class="keyword">import</span> Response</span><br><span class="line"><span class="keyword">from</span> orders.models <span class="keyword">import</span> *</span><br><span class="line"><span class="keyword">from</span> rest_framework <span class="keyword">import</span> status</span><br><span class="line">from alipay import AliPay  #支付</span><br><span class="line"><span class="keyword">from</span> mall <span class="keyword">import</span> settings</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class AliPayURLView(APIView):</span><br><span class="line">    def get(self, request, order_id):</span><br><span class="line">        # 根据order_id查询订单对象</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            order_obj = OrderInfo.objects.get(pk=order_id)</span><br><span class="line">        except:</span><br><span class="line">            raise Exception(<span class="string">&quot;订单号无效&quot;</span>)</span><br><span class="line"></span><br><span class="line">        # 创建alipay对象</span><br><span class="line">        alipay = AliPay(</span><br><span class="line">            appid=settings.ALIPAY_APPID,</span><br><span class="line">            app_notify_url=None,</span><br><span class="line">            app_private_key_path=settings.ALIPAY_PRIVATE_KEY_PATH,</span><br><span class="line">            alipay_public_key_path=settings.ALIPAY_PUBLIC_KEY_PATH,</span><br><span class="line">            debug=settings.ALIPAY_DEBUG</span><br><span class="line">        )</span><br><span class="line">    subject=<span class="string">&#x27;支付宝支付&#x27;</span></span><br><span class="line">        # 跳转到 https://openapi.alipay.com/gateway.do? + order_string</span><br><span class="line">    order_string = alipay.api_alipay_trade_page_pay(</span><br><span class="line">        out_trade_no=order_id,</span><br><span class="line">        total_amount=str(order.total_amount), # 这里类型我们要由 decimal转换为 str</span><br><span class="line">        subject=subject,</span><br><span class="line">        return_url=<span class="string">&quot;http://127.0.0.1/pay_success.html&quot;</span></span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    #  拼接url,并且返回</span><br><span class="line">    url = settings.ALIPAY_URL + <span class="string">&#x27;?&#x27;</span> + order_string</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> Response(&#123;<span class="string">&#x27;alipay_url&#x27;</span>:url&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="五、保存支付结果"><a href="#五、保存支付结果" class="headerlink" title="五、保存支付结果"></a>五、保存支付结果</h2><p>请求方式： PUT<br>路径：/pay/?参数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">class PayStatusAPIView(APIView):</span><br><span class="line">   def put(self,request):</span><br><span class="line">   # 1. 创建alipay对象</span><br><span class="line">   app_private_key_string = settings.APP_PRIVATE_KEY_PATH</span><br><span class="line">   alipay_public_key_string = settings.ALIPAY_PUBLIC_KEY_PATH</span><br><span class="line"></span><br><span class="line">   alipay = AliPay(</span><br><span class="line">       appid=settings.ALIPAY_APPID,</span><br><span class="line">       app_notify_url=None,  # 回调url</span><br><span class="line">       app_private_key_string=app_private_key_string,</span><br><span class="line">       # 支付宝的公钥，验证支付宝回传消息使用</span><br><span class="line">       alipay_public_key_string=alipay_public_key_string,</span><br><span class="line">       sign_type=<span class="string">&quot;RSA2&quot;</span>,  </span><br><span class="line">       debug=settings.ALIPAY_DEBUG  # 默认False</span><br><span class="line">   )</span><br><span class="line"></span><br><span class="line">   # 2.验证数据</span><br><span class="line">   data = request.query_params.dict()</span><br><span class="line">   # sign 不能参与签名验证</span><br><span class="line">   signature = data.pop(<span class="string">&quot;sign&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">   # verify确认是否成功</span><br><span class="line">   success = alipay.verify(data, signature)</span><br><span class="line">   <span class="keyword">if</span> success:</span><br><span class="line">       # 3.验证成功之后,可以从 data中获取 支付宝的订单id和 我们的订单id</span><br><span class="line">       # 支付宝的交易id</span><br><span class="line">       trade_id = data.get(<span class="string">&#x27;trade_id&#x27;</span>)</span><br><span class="line">       # 商家id</span><br><span class="line">       out_trade_id = data.get(<span class="string">&#x27;out_trade_id&#x27;</span>)</span><br><span class="line">       #1. 把支付宝的订单id和 我们的订单id 保存起来</span><br><span class="line">       Payment.objects.create(</span><br><span class="line">          order_id=out_trade_id,</span><br><span class="line">           trade_id=trade_id</span><br><span class="line">       )</span><br><span class="line">       # 2. 更新订单的状态</span><br><span class="line">       OrderInfo.objects.filter(order_id=out_trade_no).update(status=OrderInfo.ORDER_STATUS_ENUM[<span class="string">&#x27;UNSEND&#x27;</span>])</span><br><span class="line">       #3. 返回 支付宝的订单id</span><br><span class="line">       <span class="keyword">return</span> Response(&#123;<span class="string">&#x27;trade_id&#x27;</span>:trade_id&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>先创建沙箱账号，支付宝登录获取ALIPAY_APPID等信息，<br>保存在settings.py文件下然后进行视图的编写，<br>判断是否有下单时的订单号然后进行获取回调地址等信息，<br>最后回调返回支付成功保存数据完成支付接口</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/01/30/zy/%E5%BE%AE%E5%8D%9A%E4%B8%89%E6%96%B9%E7%99%BB%E5%BD%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/wow/2021/01/30/zy/%E5%BE%AE%E5%8D%9A%E4%B8%89%E6%96%B9%E7%99%BB%E5%BD%95/" class="post-title-link" itemprop="url">微博三方登录</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-01-30 21:01:42" itemprop="dateCreated datePublished" datetime="2021-01-30T21:01:42+08:00">2021-01-30</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:23" itemprop="dateModified" datetime="2021-04-13T15:05:23+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="微博登录介绍"><a href="#微博登录介绍" class="headerlink" title="微博登录介绍"></a>微博登录介绍</h2><p>微博登录包括身份认证、用户关系以及内容传播。允许用户使用微博帐号登录访问第三方网站，分享内容，同步信息</p>
<h2 id="什么是第三方登陆"><a href="#什么是第三方登陆" class="headerlink" title="什么是第三方登陆"></a>什么是第三方登陆</h2><p>可以避免繁琐的注册过程，让用户直接使用已有的账号系统进行登录<br>三方平台：一般是已经拥有大量用户的平台，国外的：Facebook，Twitter 国内：微博，微信，QQ</p>
<h2 id="三方登陆的优缺点"><a href="#三方登陆的优缺点" class="headerlink" title="三方登陆的优缺点"></a>三方登陆的优缺点</h2><h3 id="优点："><a href="#优点：" class="headerlink" title="优点："></a>优点：</h3><p>1）对普通用户</p>
<p>方便，快捷，方便用户实现快捷登录或注册，在第一次绑定成功之后，用户便可以实现一键登录<br>第三方登录可以实现把用户在应用内的活动同步到第三方平台上，省去了用户手动发布动态的麻烦<br>2）对应用</p>
<p>因为降低了用户的注册或登录成本，从而减少由于本地注册的繁琐性而带来的隐形用户流失，最终提高注册转化率，对某些应用来说，第三方登录可以满足自己的需要，从而增加产品知名度，</p>
<p>3）对第三方</p>
<p>增加用户对平台的依赖，使用越多，对用户的粘性越高，获得更多的影响力，有利于对平台的拉新和促活</p>
<h3 id="缺点："><a href="#缺点：" class="headerlink" title="缺点："></a>缺点：</h3><p>可能造成个人私密信息泄露</p>
<h2 id="微博第三方登陆流程"><a href="#微博第三方登陆流程" class="headerlink" title="微博第三方登陆流程"></a>微博第三方登陆流程</h2><p>前端访问我们自己的定义url，然后在我们自己的后台去调用第三方后台给的url，把后端生成的url返回给前端，也就是登录页面<br>前端使用location.href重定向获取code<br>拿着获取的code去获取的access_token（微博使用post，qq使用post，get都可以）<br>拿着access_token去获取用户openid或uid（唯一id）<br>判断用户表是不是有uid或openid，如果没有返回给用户绑定页面<br>判断输入的用户名和密码是否存在<br>不存在，创建用户，并保存到user表和第三方表<br>存在，则直接关联，需要返回令牌</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/01/29/zy/vue%E7%88%B6%E5%AD%90%E7%BB%84%E4%BB%B6%20%E5%AD%90%E7%88%B6%E7%BB%84%E4%BB%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/wow/2021/01/29/zy/vue%E7%88%B6%E5%AD%90%E7%BB%84%E4%BB%B6%20%E5%AD%90%E7%88%B6%E7%BB%84%E4%BB%B6/" class="post-title-link" itemprop="url">Hello</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-01-29 16:51:08" itemprop="dateCreated datePublished" datetime="2021-01-29T16:51:08+08:00">2021-01-29</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:46" itemprop="dateModified" datetime="2021-04-13T15:05:46+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="组件"><a href="#组件" class="headerlink" title="组件"></a>组件</h1><h2 id="什么是组件？"><a href="#什么是组件？" class="headerlink" title="什么是组件？"></a>什么是组件？</h2><p>组件（Component）是Vue.js最强大的功能之一。组件可以扩展 HTML 元素，封装可重用的代码，在较高层面上，组件是自定义元素，Vue.js 的编译器为它添加特殊功能。在有些情况下，组件也可以表现为用 is 特性进行了扩展的原生 HTML 元素。<br>所有的 Vue 组件同时也都是 Vue 的实例，所以可接受相同的选项对象 (除了一些根级特有的选项) 并提供相同的生命周期钩子。</p>
<h1 id="父子组件通信"><a href="#父子组件通信" class="headerlink" title="父子组件通信"></a>父子组件通信</h1><h3 id="父组件-Father-vue"><a href="#父组件-Father-vue" class="headerlink" title="父组件 Father.vue"></a>父组件 Father.vue</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;parent&quot;</span>&gt;</span><br><span class="line">    我是父组件</span><br><span class="line">    &lt;son :msg=<span class="string">&quot;msg&quot;</span> @say=<span class="string">&quot;fatherSay&quot;</span>&gt;&lt;/son&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">import</span> Son <span class="keyword">from</span> <span class="string">&#x27;./Son&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    data () &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      msg: <span class="string">&#x27;你好啊，儿子&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">   &#125;,</span><br><span class="line">   methods: &#123;</span><br><span class="line">      <span class="comment">// 参数就是子组件传递出来的数据</span></span><br><span class="line">      <span class="function"><span class="title">fatherSay</span>(<span class="params">msg</span>)</span>&#123;</span><br><span class="line">          <span class="built_in">console</span>.log(msg) <span class="comment">// 你好啊，父亲</span></span><br><span class="line">      &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  components:&#123;</span><br><span class="line">      son:Son</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line"></span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>
<h3 id="子组件-Son-vue"><a href="#子组件-Son-vue" class="headerlink" title="子组件 Son.vue"></a>子组件 Son.vue</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;son&quot;</span> @click=<span class="string">&quot;say&quot;</span>&gt;</span><br><span class="line">      我是子组件</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">        父组件说：&#123;&#123;msg&#125;&#125;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="comment">//父组件通过props属性传递进来的数据</span></span><br><span class="line">      props: &#123;</span><br><span class="line">          msg: &#123;</span><br><span class="line">              type: <span class="built_in">String</span>,</span><br><span class="line">              <span class="keyword">default</span>: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">                  <span class="keyword">return</span> <span class="string">&#x27;&#x27;</span></span><br><span class="line">              &#125;</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      data () &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            SonSay: <span class="string">&#x27;你好啊，父亲&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,</span><br><span class="line"></span><br><span class="line">      methods: &#123;</span><br><span class="line">          <span class="comment">// 子组件通过emit方法触发父组件中定义好的函数，从而将子组件中的数据传递给父组件</span></span><br><span class="line">          <span class="function"><span class="title">say</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">              <span class="built_in">this</span>.$emit(<span class="string">&#x27;say&#x27;</span> , <span class="built_in">this</span>.SonSay);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line"></span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>
<h2 id="总结："><a href="#总结：" class="headerlink" title="总结："></a>总结：</h2><p>父组件通过prop给子组件下发数据，子组件通过$emit触发事件给父组件发送消息，即 prop 向下传递，事件向上传递。</p>
<h1 id="子父组件通信"><a href="#子父组件通信" class="headerlink" title="子父组件通信"></a>子父组件通信</h1><h3 id="第一种方法：直接在子组件中通过this-parent-event来调用父组件的方法"><a href="#第一种方法：直接在子组件中通过this-parent-event来调用父组件的方法" class="headerlink" title="第一种方法：直接在子组件中通过this.$parent.event来调用父组件的方法"></a>第一种方法：直接在子组件中通过this.$parent.event来调用父组件的方法</h3><p>父组件 Father1.vue</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">      &lt;Son1&gt;&lt;/Son1&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">import</span> Son1 <span class="keyword">from</span> <span class="string">&#x27;./Son1&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    components:&#123;</span><br><span class="line">        Son1</span><br><span class="line">    &#125;,</span><br><span class="line">    methods:&#123;</span><br><span class="line">        <span class="function"><span class="title">father1_z</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;子组件给父组件传的值：聪明50%&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br></pre></td></tr></table></figure>
<p>子组件 Son1.vue</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">      &lt;button @click=<span class="string">&quot;son1_z()&quot;</span>&gt;点击传值给父组件&lt;/button&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    methods:&#123;</span><br><span class="line">        <span class="function"><span class="title">son1_z</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.$parent.father1_z()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line"></span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>
<h3 id="第二种方法：在子组件里用-emit向父组件触发一个事件，父组件监听这个事件就行了"><a href="#第二种方法：在子组件里用-emit向父组件触发一个事件，父组件监听这个事件就行了" class="headerlink" title="第二种方法：在子组件里用$emit向父组件触发一个事件，父组件监听这个事件就行了"></a>第二种方法：在子组件里用$emit向父组件触发一个事件，父组件监听这个事件就行了</h3><p>父组件 Father2.vue</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;father2&quot;</span>&gt;</span><br><span class="line">    &lt;son2 @son2=<span class="string">&#x27;father2_z&#x27;</span>&gt;&lt;/son2&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"> </span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">import</span> Son2 <span class="keyword">from</span> <span class="string">&#x27;./Son2&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    components: &#123;</span><br><span class="line">        Son2</span><br><span class="line">    &#125;,</span><br><span class="line">    methods: &#123;</span><br><span class="line">        <span class="function"><span class="title">father2_z</span>(<span class="params">data</span>)</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(data);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;  </span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line"></span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>
<p>子组件 Son2.vue</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;son2&quot;</span>&gt;</span><br><span class="line">    &lt;button @click=<span class="string">&quot;toFather2&quot;</span>&gt;点击传值给父组件&lt;/button&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"> </span><br><span class="line">&lt;script&gt;</span><br><span class="line">  <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    methods: &#123;</span><br><span class="line">      <span class="function"><span class="title">toFather2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">          <span class="built_in">this</span>.$emit(<span class="string">&#x27;son2&#x27;</span>,<span class="string">&#x27;子组件给父组件传的值：不赖呀，90%&#x27;</span>)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line"></span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>
<h3 id="第三种方法：父组件把方法传入子组件中，在子组件里直接调用这个方法"><a href="#第三种方法：父组件把方法传入子组件中，在子组件里直接调用这个方法" class="headerlink" title="第三种方法：父组件把方法传入子组件中，在子组件里直接调用这个方法"></a>第三种方法：父组件把方法传入子组件中，在子组件里直接调用这个方法</h3><p>父组件 Father3.vue</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;son3 :father3_z=<span class="string">&#x27;father3_z&#x27;</span>&gt;&lt;/son3&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"> </span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">import</span> Son3 <span class="keyword">from</span> <span class="string">&#x27;./Son3&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    components: &#123;</span><br><span class="line">        Son3</span><br><span class="line">    &#125;,</span><br><span class="line">    methods: &#123;</span><br><span class="line">        <span class="function"><span class="title">father3_z</span>(<span class="params">data</span>)</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;哎妈呀，您已突破聪明的极限&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;  </span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line"></span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>
<p>子组件 Son3.vue</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;button @click=<span class="string">&quot;son3_z()&quot;</span>&gt;点击传值给父组件&lt;/button&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    props: &#123;</span><br><span class="line">      father3_z: &#123;</span><br><span class="line">        type: <span class="built_in">Function</span>,</span><br><span class="line">        <span class="keyword">default</span>: <span class="literal">null</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    methods: &#123;</span><br><span class="line">      <span class="function"><span class="title">son3_z</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.father3_z) &#123;</span><br><span class="line">          <span class="built_in">this</span>.father3_z();</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line"></span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/01/28/zy/vue%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/wow/2021/01/28/zy/vue%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90/" class="post-title-link" itemprop="url">朵朵很哇塞</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-01-28 15:07:03" itemprop="dateCreated datePublished" datetime="2021-01-28T15:07:03+08:00">2021-01-28</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:49" itemprop="dateModified" datetime="2021-04-13T15:05:49+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="Vue路由钩子"><a href="#Vue路由钩子" class="headerlink" title="Vue路由钩子"></a>Vue路由钩子</h1><h2 id="一、路由钩子函数"><a href="#一、路由钩子函数" class="headerlink" title="一、路由钩子函数"></a>一、路由钩子函数</h2><p>路由钩子作用就是拦截导航栏，就是在跳转的时候进行判断<br>vue-router 提供的导航钩子主要用来拦截导航，让它完成跳转或取消<br>路由钩子分为三类： 全局的、单个路由独享的、或者组件级</p>
<h2 id="二、全局钩子"><a href="#二、全局钩子" class="headerlink" title="二、全局钩子"></a>二、全局钩子</h2><p>主要包括beforeEach和aftrEach,这类钩子主要作用于全局,一般用来判断权限,以及以及页面丢失时候需要执行的操作。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">beforeEach函数有三个参数：</span><br><span class="line">- to:router即将进入的路由对象</span><br><span class="line">- <span class="keyword">from</span>:当前导航即将离开的路由</span><br><span class="line">- next:<span class="built_in">Function</span>,进行管道中的一个钩子，如果执行完了，则导航的状态就是 confirmed （确认的）；否则为<span class="literal">false</span>，终止导航。</span><br><span class="line">可以使用router.beforeEach注册一个全局的 before 钩子</span><br><span class="line">router.beforeEach(<span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123; <span class="comment">// ... &#125;)</span></span><br><span class="line">after 钩子没有 next 方法，不能改变导航</span><br><span class="line">router.afterEach(<span class="function"><span class="params">route</span> =&gt;</span> &#123; <span class="comment">// ...&#125;)</span></span><br></pre></td></tr></table></figure>
<h2 id="三、单个路由钩子"><a href="#三、单个路由钩子" class="headerlink" title="三、单个路由钩子"></a>三、单个路由钩子</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">        path: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        component: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        meta: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        beforeEnter: <span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;     </span><br><span class="line">            <span class="comment">//进入时执行代码块</span></span><br><span class="line">        &#125;,</span><br><span class="line">        beforeLeave: <span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="comment">//离开时执行代码块</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="四、组件内的钩子"><a href="#四、组件内的钩子" class="headerlink" title="四、组件内的钩子"></a>四、组件内的钩子</h2><p>主要包括 beforeRouteEnter和beforeRouteUpdate ,beforeRouteLeave,这几个钩子都是写在组件里面也可以传三个参数(to,from,next)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">Vue.component(<span class="string">&#x27;LifeCircle&#x27;</span>,&#123;</span><br><span class="line">    template: <span class="string">&#x27;#life-circle&#x27;</span>,</span><br><span class="line">    data () &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 初始化阶段钩子函数</span></span><br><span class="line">    beforeCreate () &#123; <span class="comment">//表示组件创建前的准备工作（ 初始化事件和生命周期 ）     </span></span><br><span class="line">      <span class="comment">//组件未创建，数据拿不到</span></span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;1&#x27;</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">    created () &#123; <span class="comment">// 组件创建结束</span></span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;2&#x27;</span>)</span><br><span class="line">    &#125;,</span><br><span class="line">    beforeMount () &#123; </span><br><span class="line">      <span class="built_in">console</span>.log( <span class="string">&#x27;3&#x27;</span> )</span><br><span class="line">    &#125;,</span><br><span class="line">    mounted () &#123;     <span class="comment">//表示组件装载结束</span></span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;4&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/wow/"><i class="fa fa-angle-left" aria-label="الصفحة السابقة"></i></a><a class="page-number" href="/wow/">1</a><span class="page-number current">2</span><a class="page-number" href="/wow/page/3/">3</a><a class="extend next" rel="next" href="/wow/page/3/"><i class="fa fa-angle-right" aria-label="الصفحة التالية"></i></a>
  </nav>


<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">X y</span>
</div>
  <div class="powered-by">تطبيق الموقع <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/wow/js/utils.js"></script><script src="/wow/js/motion.js"></script><script src="/wow/js/schemes/muse.js"></script><script src="/wow/js/next-boot.js"></script>

  






  





</body>
</html>
